/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui;

import com.googlecode.jaden.common.other.Utils;
import static com.googlecode.jaden.ui.Constants.LABEL_UI_TITLE;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Splash extends JWindow {

    private final int width;
    private final int height;
    private final Image image;

    public Splash(Frame f) {
        super(f);
        image = UiUtils.getImage("images/neuron_half.png");
        ImageIcon imageIcon = new ImageIcon(image);
        width = imageIcon.getIconWidth();
        height = imageIcon.getIconHeight();
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width / 2 - width / 2,
                screenSize.height / 2 - height / 2);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                setVisible(false);
                dispose();
            }
        });

        final Runnable closerRunner = new Runnable() {
            public void run() {
                setVisible(false);
                dispose();
            }
        };

        Runnable waitRunner = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(3000);
                    SwingUtilities.invokeAndWait(closerRunner);
                } catch (Exception e) {
                    // Ignore
                }
            }
        };
        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }

    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, width, height, this);
        g.setColor(Color.cyan);
        String text = Utils.translate(LABEL_UI_TITLE) + ' ' + Constants.DIRECT_VERSION + " (" +
                Constants.DIRECT_MONIKER + ')';
        g.drawChars(text.toCharArray(), 0, text.length(), 20, height - 20);
        g.setColor(Color.darkGray);
        g.drawRect(0, 0, width - 1, height - 1);
    }

    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
}

